package com.dx.mes.service.brm.service;

import com.dx.common.table.ppt.Bis_m_item;
import com.dx.common.table.ppt.Bis_m_itemId;
import com.dx.common.table.ppt.Bis_m_label;
import com.dx.common.table.ppt.Bis_m_labelId;
import com.dx.mes.common.dao.IBisMItemDao;
import com.dx.mes.common.dao.IBisMLabelDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.brm.service.IFbpmitemlService;
import com.dx.mes.facade.brm.tx.Fbpmiteml.*;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Timestamp;
import java.util.*;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fbpmitemlService")
@SuppressWarnings({"unchecked"})
public class FbpmitemlService implements IFbpmitemlService {
	private static final String TX_NAME = T_FBPMITEML;
	private LogUtils logUtils;

	@Autowired
	private IBisMLabelDao bisMLabelDao;
	@Autowired
	private IBisMItemDao bisMItemDao;
	@Override
	public String subMainProc(String evt_no, String strInTrx) {
		AppContext.clear();
		AppContext.setCurrServiceName(FbpmitemlService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FbpmitemlService.class);
		logUtils.info("[InTrx:" + strInTrx + "]");

		Session session = bisMLabelDao.getCrSession();
		FbpmitemlO outTrx = new FbpmitemlO();
		String strOutTrx = null;

		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

		//输入对象
		try {
			FbpmitemlI inTrx = new FbpmitemlI();
			//将json字符串转换成对象
			inTrx = JacksonUtil.fromJson(strInTrx, FbpmitemlI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);

			switch (action_flg) {
				case 'Q' :
					queryByCondition(inTrx,outTrx);
					break;
				case 'D' :
					deleteFnc(inTrx,outTrx);
					break;
				case 'A':
					addFnc(inTrx,outTrx);
					break;
				case 'U':
					updateFnc(inTrx,outTrx);
					break;
				case 'I':
					queryItemFnc(inTrx,outTrx);
					break;
				case 'T':
					listAllDataId(inTrx,outTrx);
					break;		
				default:
					errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			//将一个 Object 或者List对象转化为JSOObject或者JSONArray
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}


	//查询对话框根据4个主键查询
	public void queryByCondition(FbpmitemlI inTrx,FbpmitemlO outTrx){
		try{
			FbpmitemlIA iary = inTrx.getIary().get(0);
			String toolId = iary.getTool_id_fk();
			String repUnit = iary.getRep_unit();
			String dataPat = iary.getData_pat();
			String mesIdFk = iary.getMes_id_fk();
			//站点序号

			List<FbpmitemlOA> oaryList = new ArrayList<FbpmitemlOA>();

			String hql = "FROM Bis_m_label where 1=1 ";
			if (toolId !=null && toolId.length() != 0) {
				if (toolId.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + "and tool_id_fk='" + toolId + "'";
				} else {// 模糊查询
					hql = hql + "and tool_id_fk like '" + toolId.replace("*", "%") + "'";
				}
			}
			if (repUnit != null && repUnit.length() != 0) {
				if (repUnit.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + "and rep_unit ='" + repUnit + "'";
				} else {// 模糊查询
					hql = hql + "and rep_unit like '" + repUnit.replace("*", "%") + "'";
				}
			}
			if (dataPat != null && dataPat.length() != 0) {
				if (dataPat.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + "and data_pat='" + dataPat + "'";
				} else {// 模糊查询
					hql = hql + "and data_pat like '" + dataPat.replace("*", "%") + "'";
				}
			}
			if (mesIdFk != null && mesIdFk.length() != 0) {
				if (mesIdFk.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + "and mes_id_fk='" + mesIdFk + "'";
				} else {// 模糊查询
					hql = hql + "and mes_id_fk like '" + mesIdFk.replace("*", "%") + "'";
				}
			}
			//herbinate查询出一个集合
			List<Bis_m_label> bis_m_labels = bisMLabelDao.find(hql);
			for (Bis_m_label bis_m_label : bis_m_labels) {
				FbpmitemlOA oary = new FbpmitemlOA();
				oary.setMes_id_fk(bis_m_label.getId().getMes_id_fk());
				oary.setData_pat(bis_m_label.getId().getData_pat());
				oary.setRep_unit(bis_m_label.getId().getRep_unit());
				oary.setTool_id_fk(bis_m_label.getId().getTool_id_fk());
				oaryList.add(oary);
			}
			outTrx.setOary(oaryList);
			outTrx.setTbl_cnt(oaryList.size());
		}catch (Exception ex) {
			logUtils.error("[" + Util.stackTraceToString(ex) + "]");
			outTrx.setRtn_code(E_XPLSTDAT_OTHER_ERROR + "");
			outTrx.setRtn_mesg(ex.toString());
			return;
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return;
	}

	public void queryItemFnc(FbpmitemlI inTrx,FbpmitemlO outTrx){
		FbpmitemlIA iary=inTrx.getIary().get(0);
		String dataPat = iary.getData_pat();
		String mesIdFk = iary.getMes_id_fk();
		String repUnit = iary.getRep_unit();
		String toolIdFk = iary.getTool_id_fk();

		String hql = "FROM Bis_m_item where 1=1 ";
		if (toolIdFk !=null && toolIdFk.length() != 0) {
			if (toolIdFk.indexOf("*") == -1) {// 如果不包含*,精确查询
				hql = hql + "and tool_id_fk='" + toolIdFk + "'";
			} else {// 模糊查询
				hql = hql + "and tool_id_fk like '" + toolIdFk.replace("*", "%") + "'";
			}
		}
		if (repUnit != null && repUnit.length() != 0) {
			if (repUnit.indexOf("*") == -1) {// 如果不包含*,精确查询
				hql = hql + "and rep_unit_fk ='" + repUnit + "'";
			} else {// 模糊查询
				hql = hql + "and rep_unit_fk like '" + repUnit.replace("*", "%") + "'";
			}
		}
		if (dataPat != null && dataPat.length() != 0) {
			if (dataPat.indexOf("*") == -1) {// 如果不包含*,精确查询
				hql = hql + "and data_pat_fk='" + dataPat + "'";
			} else {// 模糊查询
				hql = hql + "and data_pat_fk like '" + dataPat.replace("*", "%") + "'";
			}
		}
		if (mesIdFk != null && mesIdFk.length() != 0) {
			if (mesIdFk.indexOf("*") == -1) {// 如果不包含*,精确查询
				hql = hql + "and mes_id_fk='" + mesIdFk + "'";
			} else {// 模糊查询
				hql = hql + "and mes_id_fk like '" + mesIdFk.replace("*", "%") + "'";
			}
		}
		hql += " order by data_seq";

		List<Bis_m_item> bis_m_items= bisMItemDao.find(hql);
		List<FbpmitemlOB> oaryList = new ArrayList<FbpmitemlOB>();
		for(int i = 0; i < bis_m_items.size(); i++){
			FbpmitemlOB oary = new FbpmitemlOB();
			oary.setData_id(bis_m_items.get(i).getId().getData_id());
			oary.setData_dsc(bis_m_items.get(i).getData_dsc());
			oary.setData_group(bis_m_items.get(i).getData_group());
			oary.setData_group_seq(bis_m_items.get(i).getData_group_seq());
			oary.setData_attr(bis_m_items.get(i).getData_attr());
			oary.setSpec_chk_flg(bis_m_items.get(i).getSpec_chk_flg());
			if(bis_m_items.get(i).getU_spec() !=null){
				oary.setU_spec(bis_m_items.get(i).getU_spec().toString());
			}
			if(bis_m_items.get(i).getL_spec() !=null){
				oary.setL_spec(bis_m_items.get(i).getL_spec().toString());
			}
			oary.setOpi_data_flg(bis_m_items.get(i).getOpi_data_flg());
			oary.setSpec_chk_typ(bis_m_items.get(i).getSpec_chk_typ());
			oary.setData_type(bis_m_items.get(i).getData_type());
			oaryList.add(oary);
		}

//
//		Set<String> dataIds = new TreeSet<String>();
//		for(int i = 0 ;i < bis_m_items.size(); i++){
//			dataIds.add(bis_m_items.get(i).getId().getData_id());
//		}
//		List<FbpmitemlOB> oaryList = new ArrayList<FbpmitemlOB>();
//		List<String> dataStrs = new ArrayList<String>();
//
//		Iterator<String> iterator = dataIds.iterator();
//		while(iterator.hasNext()){
//			dataStrs.add(iterator.next());
//		}
//		Collections.sort(dataStrs,new Comparator() {
//			@Override
//			public int compare(Object o1, Object o2) {
//				return new Double((String) o1).compareTo(new Double((String) o2));
//			}
//		});
//		for(int i = 0; i< dataStrs.size(); i++){
//			for (int j = 0;j < bis_m_items.size() ;j++){
//				if(dataStrs.get(i).equals(bis_m_items.get(j).getId().getData_id())){
//					FbpmitemlOB oary = new FbpmitemlOB();
//					oary.setData_id(bis_m_items.get(j).getId().getData_id());
//					oary.setData_dsc(bis_m_items.get(j).getData_dsc());
//					oary.setData_group(bis_m_items.get(j).getData_group());
//					oary.setData_group_seq(bis_m_items.get(j).getData_group_seq());
//					oary.setData_attr(bis_m_items.get(j).getData_attr());
//					oary.setSpec_chk_flg(bis_m_items.get(j).getSpec_chk_flg());
//					if(bis_m_items.get(j).getU_spec() !=null){
//						oary.setU_spec(bis_m_items.get(j).getU_spec().toString());
//					}
//					if(bis_m_items.get(j).getL_spec() !=null){
//						oary.setL_spec(bis_m_items.get(j).getL_spec().toString());
//					}
//					oary.setOpi_data_flg(bis_m_items.get(j).getOpi_data_flg());
//					oary.setSpec_chk_typ(bis_m_items.get(j).getSpec_chk_typ());
//					oaryList.add(oary);
//				}else{
//					continue;
//				}
//			}
//		}
		outTrx.setOaryB(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return;
	}

	public void listAllDataId(FbpmitemlI inTrx,FbpmitemlO outTrx){
		String hql = "FROM Bis_m_item where 1 = 1";
		List<Bis_m_item> bis_m_items= bisMItemDao.find(hql);
		Set<String> dataIds = new TreeSet<String>();
		for (Bis_m_item bis_m_item : bis_m_items) {
			dataIds.add(bis_m_item.getId().getData_id());
		}

		List<FbpmitemlOB> oaryList = new ArrayList<FbpmitemlOB>();
		List<String> dataStrs = new ArrayList<String>();

		Iterator<String> iterator = dataIds.iterator();
		while(iterator.hasNext()){
			dataStrs.add(iterator.next());
		}
		Collections.sort(dataStrs,new Comparator() {
			@Override
			public int compare(Object o1, Object o2) {
				return new Double((String) o1).compareTo(new Double((String) o2));
			}
		});
		for (String string : dataStrs) {
			FbpmitemlOB oary = new FbpmitemlOB();
			oary.setData_id(string);
			oaryList.add(oary);
		}

		outTrx.setOaryB(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
	}

	//删除操作
	public void deleteFnc(FbpmitemlI inTrx,FbpmitemlO outTrx){
		FbpmitemlIA iary=inTrx.getIary().get(0);
		String toolId = iary.getTool_id_fk();
		String repUnit = iary.getRep_unit();
		String dataPat = iary.getData_pat();
		String mesIdFk = iary.getMes_id_fk();
		String mdlIdFk = iary.getMdl_id_fk();
		Bis_m_labelId bis_m_labelId= new Bis_m_labelId();

		bis_m_labelId.setData_pat(dataPat);
		bis_m_labelId.setTool_id_fk(toolId);
		bis_m_labelId.setRep_unit(repUnit);
		bis_m_labelId.setMes_id_fk(mesIdFk);

		Bis_m_label bis_m_label = bisMLabelDao.get(bis_m_labelId);

		if(bis_m_label == null){
			outTrx.setRtn_code(E_FPBISPAM_OTHER_ERR + "");
			outTrx.setRtn_mesg("not found record");
			return;
		}else{
			bisMLabelDao.delete(bis_m_label);
		}

		//delete items
		String hql = "FROM Bis_m_item where 1=1 ";
		hql = hql +" and tool_id_fk = '" +toolId+"'"+ " and rep_unit_fk ='"
				+ repUnit +"'" + " and data_pat_fk = '" +dataPat+"'"
				+ " and mes_id_fk ='" +mesIdFk+"'";
		List<Bis_m_item> bis_m_items= bisMItemDao.find(hql);
		for (Bis_m_item bis_m_item : bis_m_items) {
			bisMItemDao.delete(bis_m_item);
		}

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return;
	}

	//删除ITEM操作 对应toolID
/*	public void deleteItemFnc(FbpmitemlI inTrx,FbpmitemlO outTrx){
		FbpmitemlIB iary=inTrx.getIaryB().get(0);
		String dataId = iary.getData_id();
		String toolIdFk = iary.getTool_id_fk();
		String repUnitFk  = iary.getRep_unit_fk();
		String dataPatFk = iary.getData_pat_fk();
		String mesIdFk = iary.getMes_id_fk();

		Bis_m_itemId id = new Bis_m_itemId();
		id.setData_id(dataId);
		id.setData_pat_fk(dataPatFk);
		id.setTool_id_fk(toolIdFk);
		id.setRep_unit_fk(repUnitFk);
		id.setMes_id_fk(mesIdFk);

		Bis_m_item bis_m_item=bisMItemDao.get(id);
		if (bis_m_item ==null) {
			outTrx.setRtn_code(E_FPBISPAM_OTHER_ERR + "");
			outTrx.setRtn_mesg("not found record");
			return;
		}
		bisMItemDao.delete(bis_m_item);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
	}*/

	//新增操作
	public void addFnc(FbpmitemlI inTrx,FbpmitemlO outTrx){
		String evtUsr = inTrx.getEvt_usr();
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		if(inTrx.getIary().size() > 0){
			FbpmitemlIA iary=inTrx.getIary().get(0);
			String dataPat = iary.getData_pat();
			String mesIdFk = iary.getMes_id_fk();
			String repUnit = iary.getRep_unit();
			String toolIdFk = iary.getTool_id_fk();

			Bis_m_labelId  id = new Bis_m_labelId();
			id.setData_pat(dataPat);
			id.setMes_id_fk(mesIdFk);
			id.setRep_unit(repUnit);
			id.setTool_id_fk(toolIdFk);

			Bis_m_label bis_m_label = bisMLabelDao.get(id);
			if(bis_m_label != null){
				outTrx.setRtn_code(E_FPBISPAM_RECORDER_ALREADY_EXIST+ "");
				outTrx.setRtn_mesg("bis_m_label already Exist");
				return;
			}
			Bis_m_label newbisMLabel = new Bis_m_label();
			newbisMLabel.setId(id);
			newbisMLabel.setEvt_user(evtUsr);
			newbisMLabel.setEvt_timestamp(current_timestamp);

			bisMLabelDao.save(newbisMLabel);
			FbpmitemlOA oary = new FbpmitemlOA();
			oary.setData_pat(dataPat);
			oary.setMes_id_fk(mesIdFk);
			oary.setRep_unit(repUnit);
			oary.setTool_id_fk(toolIdFk);

			List<FbpmitemlOA> oaryList =new ArrayList<FbpmitemlOA>();
			oaryList.add(oary);
			outTrx.setOary(oaryList);
		}
		List<FbpmitemlIB> iaryBList = inTrx.getIaryB();
		List<FbpmitemlOB> oaryBList = new ArrayList<FbpmitemlOB>();
		//item
		if(iaryBList.size() > 0){
			FbpmitemlIB ib = iaryBList.get(0);
			// 删除bis_m_item表中所有记录
			String hql = "FROM Bis_m_item where 1=1 ";
			hql = hql +" and tool_id_fk = '" + ib.getTool_id_fk() + "'" + " and rep_unit_fk ='"
					+ ib.getRep_unit_fk() + "'" + " and data_pat_fk ='" + ib.getData_pat_fk() + "'" + " and mes_id_fk ='"
					+ ib.getMes_id_fk() + "'" ;
			List<Bis_m_item> bisMItemList = bisMItemDao.find(hql);
			for (Bis_m_item bis_m_item : bisMItemList) {
				bisMItemDao.delete(bis_m_item);
			}
			for (FbpmitemlIB iaryB : iaryBList) {
				String toolIdFk = iaryB.getTool_id_fk();
				String repUnitFk =iaryB.getRep_unit_fk();
				String dataPatFk =iaryB.getData_pat_fk();
				String mesIdFk =iaryB.getMes_id_fk();
				String mdlIdFk = iaryB.getMdl_id_fk();
				String dataId =iaryB.getData_id();
				Integer dataSeq = iaryB.getData_seq();
				String dataDsc =iaryB.getData_dsc();
				String dataGroup = iaryB.getData_group();
				String dataGroupSeq =iaryB.getData_group_seq();
				String dataAttr =iaryB.getData_attr();
				String specChkFlg =iaryB.getSpec_chk_flg();
				String uSpec =iaryB.getU_spec();
				String lSpec =iaryB.getL_spec();
				String opiDataFlg =iaryB.getOpi_data_flg();
				String specChkTyp =iaryB.getSpec_chk_typ();
				String dataType = iaryB.getData_type();

				Bis_m_itemId id = new Bis_m_itemId();
				id.setTool_id_fk(toolIdFk);
				id.setRep_unit_fk(repUnitFk);
				id.setData_pat_fk(dataPatFk);
				id.setMes_id_fk(mesIdFk);
				id.setData_id(dataId);

				Bis_m_item bis_m_item = bisMItemDao.get(id);
				Bis_m_item nbis_m_item = new Bis_m_item();
				if(bis_m_item != null){
					bis_m_item.setId(id);
					bis_m_item.setData_group(dataGroup);
					bis_m_item.setData_dsc(dataDsc);
					if(!StringUtils.isEmpty(dataGroupSeq)){
						bis_m_item.setData_group_seq(dataGroupSeq);
					}
					bis_m_item.setData_attr(dataAttr);
					bis_m_item.setSpec_chk_flg(specChkFlg);
					if(specChkFlg.equals("Y")){
						if(!StringUtils.isEmpty(uSpec)){
							bis_m_item.setU_spec(Double.parseDouble(uSpec));
						}
						if(!StringUtils.isEmpty(lSpec)){
							bis_m_item.setL_spec(Double.parseDouble(lSpec));
						}
						if(!StringUtils.isEmpty(specChkTyp)){
							bis_m_item.setSpec_chk_typ(specChkTyp);
						}
					}else{
						bis_m_item.setU_spec(null);
						bis_m_item.setL_spec(null);
						bis_m_item.setSpec_chk_typ(_SPACE);
					}

					if(!StringUtils.isEmpty(opiDataFlg)){
						bis_m_item.setOpi_data_flg(opiDataFlg);
					}
					bis_m_item.setData_seq(dataSeq);
					bis_m_item.setData_type(dataType);
					bis_m_item.setEvt_usr(evtUsr);
					bis_m_item.setEvt_timestamp(current_timestamp);
					bisMItemDao.update(bis_m_item);
				}else{
					nbis_m_item.setId(id);
					nbis_m_item.setData_group(dataGroup);
					nbis_m_item.setData_dsc(dataDsc);
					if(!StringUtils.isEmpty(dataGroupSeq)){
						nbis_m_item.setData_group_seq(dataGroupSeq);
					}
					nbis_m_item.setData_attr(dataAttr);
					nbis_m_item.setSpec_chk_flg(specChkFlg);
					if(specChkFlg.equals("Y")){
						if(!StringUtils.isEmpty(uSpec)){
							nbis_m_item.setU_spec(Double.parseDouble(uSpec));
						}
						if(!StringUtils.isEmpty(lSpec)){
							nbis_m_item.setL_spec(Double.parseDouble(lSpec));
						}
						if(!StringUtils.isEmpty(specChkTyp)){
							nbis_m_item.setSpec_chk_typ(specChkTyp);
						}
					}

					if(!StringUtils.isEmpty(opiDataFlg)){
						nbis_m_item.setOpi_data_flg(opiDataFlg);
					}
					nbis_m_item.setData_seq(dataSeq);
					nbis_m_item.setData_type(dataType);
					nbis_m_item.setEvt_timestamp(current_timestamp);
					nbis_m_item.setEvt_usr(evtUsr);
					bisMItemDao.save(nbis_m_item);
				}

				FbpmitemlOB oary = new FbpmitemlOB();
				oary.setData_id(dataId);
				oary.setData_dsc(dataDsc);
				oary.setData_group_seq(dataGroupSeq);
				oary.setData_attr(dataAttr);
				oary.setSpec_chk_flg(specChkFlg);
				oary.setU_spec(uSpec.toString());
				oary.setL_spec(lSpec.toString());
				oary.setOpi_data_flg(opiDataFlg);
				oary.setSpec_chk_typ(specChkTyp);
				oaryBList.add(oary);
			}
			outTrx.setOaryB(oaryBList);
		}

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}

	//修改操作
	public void updateFnc(FbpmitemlI inTrx,FbpmitemlO outTrx){
		String evtUsr = inTrx.getEvt_usr();
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		FbpmitemlIA iary=inTrx.getIary().get(0);
		String dataPat = iary.getData_pat();
		String mesIdFk = iary.getMes_id_fk();
		String repUnit = iary.getRep_unit();
		String toolIdFk = iary.getTool_id_fk();
		String mdlIdFk = iary.getMdl_id_fk();
		Bis_m_labelId  id = new Bis_m_labelId();
		id.setData_pat(dataPat);
		id.setMes_id_fk(mesIdFk);
		id.setRep_unit(repUnit);
		id.setTool_id_fk(toolIdFk);

		Bis_m_label bis_m_label = bisMLabelDao.get(id);
		if(bis_m_label == null){
			outTrx.setRtn_code(E_FPBISPAM_RECORDER_ALREADY_EXIST+ "");
			outTrx.setRtn_mesg("没有找到该数据信息！");
			return;
		}

		bis_m_label.setId(id);
		bis_m_label.setEvt_user(evtUsr);
		bis_m_label.setEvt_timestamp(current_timestamp);
		bisMLabelDao.update(bis_m_label);

		FbpmitemlOA oary = new FbpmitemlOA();
		oary.setData_pat(dataPat);
		oary.setMes_id_fk(mesIdFk);
		oary.setRep_unit(repUnit);
		oary.setTool_id_fk(toolIdFk);
		oary.setMdl_id_fk(mdlIdFk);

		List<FbpmitemlOA> oaryList =new ArrayList<FbpmitemlOA>();
		oaryList.add(oary);
		outTrx.setOary(oaryList);


		// 删除bis_m_item表中所有记录
		String hql = "FROM Bis_m_item where 1=1 ";
		hql = hql +" and tool_id_fk = '" + toolIdFk + "'" + " and rep_unit_fk ='"
				+ repUnit + "'" + " and data_pat_fk ='" + dataPat + "'" + " and mes_id_fk ='"
				+ mesIdFk + "'" ;
		List<Bis_m_item> bisMItemList = bisMItemDao.find(hql);
		for (Bis_m_item bis_m_item : bisMItemList) {
			bisMItemDao.delete(bis_m_item);
		}
		//item
		List<FbpmitemlIB> iaryBList = inTrx.getIaryB();
		List<FbpmitemlOB> oaryBList = new ArrayList<FbpmitemlOB>();
		if(iaryBList.size() > 0){
			for (FbpmitemlIB iaryB : iaryBList) {
				String toolIdFk2 = iaryB.getTool_id_fk();
				String repUnitFk =iaryB.getRep_unit_fk();
				String dataPatFk =iaryB.getData_pat_fk();
				String mesIdFk2 =iaryB.getMes_id_fk();
				String mdlIdFk2 = iaryB.getMdl_id_fk();
				String dataId =iaryB.getData_id();
				Integer dataSeq = iaryB.getData_seq();
				String dataDsc =iaryB.getData_dsc();
				String dataGroup = iaryB.getData_group();
				String dataGroupSeq =iaryB.getData_group_seq();
				String dataAttr =iaryB.getData_attr();
				String specChkFlg =iaryB.getSpec_chk_flg();
				String uSpec =iaryB.getU_spec();
				String lSpec =iaryB.getL_spec();
				String opiDataFlg =iaryB.getOpi_data_flg();
				String specChkTyp =iaryB.getSpec_chk_typ();
				String dataType = iaryB.getData_type();

				Bis_m_itemId id2 = new Bis_m_itemId();
				id2.setTool_id_fk(toolIdFk2);
				id2.setRep_unit_fk(repUnitFk);
				id2.setData_pat_fk(dataPatFk);
				id2.setMes_id_fk(mesIdFk2);
				id2.setData_id(dataId);

				Bis_m_item bis_m_item = bisMItemDao.get(id2);
				Bis_m_item nbis_m_item = new Bis_m_item();
				if(bis_m_item != null){
					bis_m_item.setId(id2);
					bis_m_item.setData_group(dataGroup);
					bis_m_item.setData_dsc(dataDsc);
					if(dataGroupSeq != null){
						bis_m_item.setData_group_seq(dataGroupSeq);
					}
					bis_m_item.setData_attr(dataAttr);
					bis_m_item.setSpec_chk_flg(specChkFlg);
					if(specChkFlg.equals("Y")){
						if(!StringUtils.isEmpty(uSpec)){
							bis_m_item.setU_spec(Double.parseDouble(uSpec));
						}
						if(!StringUtils.isEmpty(lSpec)){
							bis_m_item.setL_spec(Double.parseDouble(lSpec));
						}
						if(!StringUtils.isEmpty(specChkTyp)){
							bis_m_item.setSpec_chk_typ(specChkTyp);
						}
					}else {
						bis_m_item.setU_spec(null);
						bis_m_item.setL_spec(null);
						bis_m_item.setSpec_chk_typ(_SPACE);
					}

					if(!StringUtils.isEmpty(opiDataFlg)){
						bis_m_item.setOpi_data_flg(opiDataFlg);
					}
					bis_m_item.setData_seq(dataSeq);
					bis_m_item.setData_type(dataType);
					bis_m_item.setEvt_timestamp(current_timestamp);
					bis_m_item.setEvt_usr(evtUsr);
					bisMItemDao.update(bis_m_item);
				}else{
					nbis_m_item.setId(id2);
					nbis_m_item.setData_group(dataGroup);
					nbis_m_item.setData_dsc(dataDsc);
					if(dataGroupSeq != null){
						nbis_m_item.setData_group_seq(dataGroupSeq);
					}
					nbis_m_item.setData_attr(dataAttr);
					nbis_m_item.setSpec_chk_flg(specChkFlg);
					if(specChkFlg.equals("Y")){
						if(!StringUtils.isEmpty(uSpec)){
							nbis_m_item.setU_spec(Double.parseDouble(uSpec));
						}
						if(!StringUtils.isEmpty(lSpec)){
							nbis_m_item.setL_spec(Double.parseDouble(lSpec));
						}
						if(!StringUtils.isEmpty(specChkTyp)){
							nbis_m_item.setSpec_chk_typ(specChkTyp);
						}
					}

					if(!StringUtils.isEmpty(opiDataFlg)){
						nbis_m_item.setOpi_data_flg(opiDataFlg);
					}
					nbis_m_item.setData_seq(dataSeq);
					nbis_m_item.setData_type(dataType);
					nbis_m_item.setEvt_usr(evtUsr);
					nbis_m_item.setEvt_timestamp(current_timestamp);
					bisMItemDao.save(nbis_m_item);
				}
				FbpmitemlOB oaryB= new FbpmitemlOB();
				oaryB.setData_id(dataId);
				oaryB.setData_dsc(dataDsc);
				oaryB.setData_group_seq(dataGroupSeq);
				oaryB.setData_attr(dataAttr);
				oaryB.setSpec_chk_flg(specChkFlg);
				oaryB.setU_spec(uSpec.toString());
				oaryB.setL_spec(lSpec.toString());
				oaryB.setOpi_data_flg(opiDataFlg);
				oaryB.setSpec_chk_typ(specChkTyp);
				oaryBList.add(oaryB);
			}
			outTrx.setOaryB(oaryBList);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}


	//错误信息
	public void errActionFnc(FbpmitemlO outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}
}
